﻿using System;
using System.Collections.Generic;
using System.Web;
using DeviceInfoManage.DataAccessHelper; /*引入字符串函数类的命名空间*/
using DeviceInfoManage.DataAccessLayer;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DeviceInfoManage.BusinessLogicLayer
{
    /// <summary>
    /// 文件管理
    /// </summary>
    public class FileManage
    {
        #region 字段属性
        private int _fileid;
        private string _filename;
        private string _filetype;
        private DateTime _uploadtime;
        private string _uploaduser;
        private string _filepath;
        /// <summary>
        /// 文件ID
        /// </summary>
        public int fileId
        {
            set { _fileid = value; }
            get { return _fileid; }
        }
        /// <summary>
        /// 文件名称
        /// </summary>
        public string fileName
        {
            set { _filename = value; }
            get { return _filename; }
        }
        /// <summary>
        /// 文件类型
        /// </summary>
        public string fileType
        {
            set { _filetype = value; }
            get { return _filetype; }
        }
        /// <summary>
        /// 上传时间
        /// </summary>
        public DateTime uploadTime
        {
            set { _uploadtime = value; }
            get { return _uploadtime; }
        }
        /// <summary>
        /// 上传人员
        /// </summary>
        public string uploadUser
        {
            set { _uploaduser = value; }
            get { return _uploaduser; }
        }

        /// <summary>
        /// 文件路径
        /// </summary>
        public string filePath
        {
            set { _filepath = value; }
            get { return _filepath; }
        }
        #endregion

        #region 构造函数
        public FileManage()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        #endregion

        #region  成员方法
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("fileId", "fileManage");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int fileId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from fileManage");
            strSql.Append(" where fileId=@fileId ");
            SqlParameter[] parameters = {
					new SqlParameter("@fileId", SqlDbType.Int,4)};
            parameters[0].Value = fileId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(FileManage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into fileManage(");
            strSql.Append("fileName,fileType,uploadTime,uploadUser,filePath)");
            strSql.Append(" values (");
            strSql.Append("@fileName,@fileType,@uploadTime,@uploadUser,@filePath)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@fileName", SqlDbType.VarChar,200),
					new SqlParameter("@fileType", SqlDbType.VarChar,5),
					new SqlParameter("@uploadTime", SqlDbType.DateTime),
					new SqlParameter("@uploadUser", SqlDbType.VarChar,50),
                    new SqlParameter("@filePath",SqlDbType.VarChar,500)};
            parameters[0].Value = model.fileName;
            parameters[1].Value = model.fileType;
            parameters[2].Value = model.uploadTime;
            parameters[3].Value = model.uploadUser;
            parameters[4].Value = model.filePath;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int fileId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from fileManage ");
            strSql.Append(" where fileId=@fileId ");
            SqlParameter[] parameters = {
					new SqlParameter("@fileId", SqlDbType.Int,4)};
            parameters[0].Value = fileId;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FileManage GetModel(int fileId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 fileId,fileName,fileType,uploadTime,uploadUser,filePath from fileManage ");
            strSql.Append(" where fileId=@fileId ");
            SqlParameter[] parameters = {
					new SqlParameter("@fileId", SqlDbType.Int,4)};
            parameters[0].Value = fileId;

            FileManage model = new FileManage();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["fileId"].ToString() != "")
                {
                    model.fileId = int.Parse(ds.Tables[0].Rows[0]["fileId"].ToString());
                }
                model.fileName = ds.Tables[0].Rows[0]["fileName"].ToString();
                model.fileType = ds.Tables[0].Rows[0]["fileType"].ToString();
                if (ds.Tables[0].Rows[0]["uploadTime"].ToString() != "")
                {
                    model.uploadTime = DateTime.Parse(ds.Tables[0].Rows[0]["uploadTime"].ToString());
                }
                model.uploadUser = ds.Tables[0].Rows[0]["uploadUser"].ToString();
                model.filePath = ds.Tables[0].Rows[0]["filePath"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select fileId,fileName,fileType,uploadTime,uploadUser,filePath ");
            strSql.Append(" FROM fileManage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" fileId,fileName,fileType,uploadTime,uploadUser,filePath ");
            strSql.Append(" FROM fileManage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  成员方法
    }
}
